package com.rundatop.core.advice;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.client.ResourceAccessException;

import com.rundatop.core.entity.DataEntity;

@RestControllerAdvice(basePackages=" com.rundatop.*.controller")
@ResponseBody
public class GlobalExceptionHandler {
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	
	@ExceptionHandler(value=ResourceAccessException.class)  
	@ResponseStatus(code=HttpStatus.NOT_FOUND)
	public DataEntity ResourceAccessExceptionHandler(HttpServletRequest request,  
			Exception exception) throws Exception  
	{  
		exception.printStackTrace();
		logger.error("我报错了："+exception.getLocalizedMessage());
		logger.error("我报错了："+exception.getCause());
		logger.error("我报错了："+exception.getSuppressed());
		logger.error("我报错了："+exception.getMessage());
		logger.error("我报错了："+exception.getStackTrace());
		return DataEntity.getInstance("8106", false, "服务器或数据异常，请稍后再试");
	}
	
	@ExceptionHandler(value=Exception.class)  
	@ResponseStatus(code=HttpStatus.OK)
	public DataEntity allExceptionHandler(HttpServletRequest request,  
            Exception exception) throws Exception  
    {  
        exception.printStackTrace();
        logger.error("我报错了："+exception.getLocalizedMessage());
        logger.error("我报错了："+exception.getCause());
        logger.error("我报错了："+exception.getSuppressed());
        logger.error("我报错了："+exception.getMessage());
        logger.error("我报错了："+exception.getStackTrace());
        return DataEntity.getInstance("8106", false, "服务器或数据异常，请稍后再试");
    }

}
